//
//  TimeExpansion.m
//  ExpandTest
//
//  Created by Mac on 15/12/16.
//  Copyright © 2015年 lc. All rights reserved.
//

#import "TimeExpansion.h"

@implementation TimeExpansion

#pragma mark 时间戳转为时间
/**
 *  <#Description#>
 *
 *  @param timestamp 时间戳字符串
 *  @param fomatter  时间格式
 *
 *  @return 时间
 */
+(NSString *)timestampToTime:(NSString *)timestamp formatter:(NSString *)formatter{
    NSInteger value = [timestamp integerValue];
    NSDateFormatter * NSformatter =[[NSDateFormatter alloc]init];
    [NSformatter setDateStyle:NSDateFormatterMediumStyle];
    [NSformatter setTimeStyle:NSDateFormatterShortStyle];
    if (formatter==nil) {
        [NSformatter setDateFormat:@"YYYY.MM.dd HH:mm:ss"];
    }else{
        [NSformatter setDateFormat:formatter];
    }
    NSDate  *date = [NSDate dateWithTimeIntervalSince1970:value];
    NSString  *time = [NSformatter stringFromDate:date];
    return time;
}

#pragma mark 取得当前时间
+(NSString *)nowTimeWithFormatter:(NSString *)formatter type:(TimeType)type{
    NSDateFormatter* NSformatter = [[NSDateFormatter alloc]init];
    
    if (formatter==nil) {
        [NSformatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    }else{
        [NSformatter setDateFormat:formatter];
    }
    
    NSString* date= [NSformatter stringFromDate:[NSDate date]];
    NSDate* nowTime = [NSDate date];
    NSCalendar *cal = [NSCalendar currentCalendar];
    unsigned int unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute |NSCalendarUnitSecond;
    NSDateComponents *dt = [cal components:unitFlags fromDate:nowTime];
    NSInteger y = [dt year];
    NSInteger m = [dt month];
    NSInteger d = [dt day];
    NSInteger hour = [dt hour];
    NSInteger min = [dt minute];
    NSInteger sec = [dt second];
    NSString * time;
    switch (type){
        case Time_Year:
            time=[NSString stringWithFormat:@"%ld",(long)y];
            break;
        case Time_Month:
            time=[NSString stringWithFormat:@"%ld",(long)m];
            break;
        case Time_Day:
            time=[NSString stringWithFormat:@"%ld",(long)d];
            break;
        case Time_Hour:
            time=[NSString stringWithFormat:@"%ld",(long)hour];
            break;
        case Time_Miute:
            time=[NSString stringWithFormat:@"%ld",(long)min];
            break;
        case Time_Second:
            time=[NSString stringWithFormat:@"%ld",(long)sec];
            break;
        default:
            time=date;
            break;
    }
    return time;
}

// 获取时间戳

+(NSString *)nowTimeStamp{
     return [NSString stringWithFormat:@"%.0f", [[NSDate date] timeIntervalSince1970]];
}

@end
